const app = getApp()
Page({
  data: {
    imageList: [],
    fileidList: new Array(),
    loading: false,
    xwBt: '',
    xwNr: ''
  },
  chooseImage() {
    const that = this
    wx.chooseImage({
      sourceType: ['camera', 'album'],
      sizeType: ['compressed', 'original'],
      count: 7,
      success(res) {
        const filePath = res.tempFilePaths[0]
        that.setData({
          imageList: res.tempFilePaths
        })
      }
    })
  },

  previewImage(e) {
    const current = e.target.dataset.src

    wx.previewImage({
      current,
      urls: this.data.imageList
    })
  },
  funBt: function(e) {
    this.setData({
      xwBt: e.detail.value
    })
  },
  funNr: function(e) {
    this.setData({
      xwNr: e.detail.value
    })
  },

  funFb: function(e) {
    if (this.data.xwBt == '') {
      wx.showToast({
        title: '请输入新闻标题！',
        icon: 'none'
      })
      this.setData({
        loading: false
      })
      return false
    }
    if (this.data.xwNr == '') {
      wx.showToast({
        title: '请输入新闻内容！',
        icon: 'none'
      })
      this.setData({
        loading: false
      })
      return false
    }
    this.setData({
      loading: true
    })
    const that = this
    var flag = 0;
    for (var i = 0; i < that.data.imageList.length; i++) {
      const filePath = that.data.imageList[i]
      const cloudPath = 'News/' + Date.parse(new Date()) + i + filePath.match(/\.[^.]+?$/)[0]
      wx.cloud.uploadFile({
        cloudPath: cloudPath,
        filePath: filePath
      }).then(res => {
        that.data.fileidList.push(res.fileID)
        flag++
      }).catch(err => {
        wx.showToast({
          icon: 'none',
          title: '图片上传失败'
        })
        this.setData({
          loading: false
        })
        return false
      })
    }
    var node = this
    var timer = setInterval(function() {

      if (flag == that.data.imageList.length) {
        const Ymd = new Date()
        wx.cloud.database({
          env: 'gtss-2b1812'
        }).collection('News').add({
          data: {
            xwBt: node.data.xwBt,
            xwNr: node.data.xwNr,
            fileidList: node.data.fileidList,
            time: Date.parse(new Date()),
            dateYmd: Ymd.getFullYear() + '/' + (Ymd.getMonth() + 1) + '/' + Ymd.getDate(),
            dept: app.globalData.userDept
          },
          success(res) {
            node.setData({
              loading: false
            })
            wx.showToast({
              title: '发布成功！',
              icon: 'success',
              duration: 2000
            })
            node.setData({
              imageList: [],
              fileidList: [],
              xwBt: '',
              xwNr: ''
            })
          },
          fail() {
            node.setData({
              loading: false
            })
            wx.showModal({
              title: '发布失败！',
              content: '抱歉！由于小程序数据库问题，导致本次新闻发布失败，请重启微信后重试。'
            })
            return false
          }
        })
        clearInterval(timer)
        setTimeout(function() {
          wx.cloud.database({
            env: 'gtss-2b1812'
          }).collection('News').get({
            success(res) {}
          })
        }, 1000)
      } else {}
    }, 100)

  }
})